﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tea.DB;
using tea.BL;
using tea.Models;

namespace tea.Controllers
{
    [Authorize(Roles = BL사용자.ADMIN_OR_STAFF)]
    public class BbsAdminController : Controller
    {
        [HttpGet]
        public ActionResult Edit(int id)
        {
            using (var db = new DBDataContext()) {
                var 게시판 = db.게시판.Single(p => p.ID == id);
                var 모델 = new BbsAdminEditModel() {
                    게시글분류목록 = BL게시판.게시글분류목록(id),
                    게시판 = 게시판
                };
                return View(모델);
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult UpdateTitle(int id, string title)
        {
            using (var db = new DBDataContext()) {
                var bbs = db.게시판.Single(p => p.ID == id);
                bbs.제목 = title;
                db.SubmitChanges();
                return Json(new AjaxSuccessModel(null, Consts.저장됨));
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult DeleteCategory(int id)
        {
            using (var db = new DBDataContext()) {
                var 항목 = db.게시글분류.Single(p => p.ID == id);
                if (항목.게시글.Count > 0) return Json(new AjaxFailureModel(Consts.게시글삭제실패));
                if (항목.제목 == Consts.미분류) return Json(new AjaxFailureModel(Consts.게시글삭제실패));

                db.게시글분류.DeleteOnSubmit(항목);
                db.SubmitChanges();
                return Json(new AjaxSuccessModel(Url.BbsAdminEdit(항목.게시판ID)));
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult UpdateCategory(int id, string title)
        {
            using (var db = new DBDataContext()) {
                var 항목 = db.게시글분류.Single(p => p.ID == id);
                항목.제목 = title;
                db.SubmitChanges();
                return Json(new AjaxSuccessModel(null, Consts.저장됨));
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult AddCategory(int id, string title)
        {
            using (var db = new DBDataContext()) {
                var bbs = db.게시판.Single(p => p.ID == id);
                var 항목 = new 게시글분류 { 게시판ID = id, 제목 = title };
                db.게시글분류.InsertOnSubmit(항목);
                db.SubmitChanges();
                return Json(new AjaxSuccessModel(Url.BbsAdminEdit(bbs.ID)));
            }
        }


        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult New()
        {
            using (var db = new DBDataContext()) {
                var bbs = new 게시판 { 제목 = Consts.새제목 };
                db.게시판.InsertOnSubmit(bbs);
                db.SubmitChanges();

                db.게시글분류.InsertOnSubmit(new 게시글분류 { 게시판ID = bbs.ID, 제목 = Consts.미분류 });
                db.SubmitChanges();

                return Json(new AjaxSuccessModel(Url.BbsAdminEdit(bbs.ID)));
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult Delete(int id)
        {
            using (var db = new DBDataContext()) {
                var bbs = db.게시판.Single(p => p.ID == id);
                if (bbs.게시글.Count > 0) return Json(new AjaxFailureModel(Consts.게시글삭제실패));
                
                db.게시글분류.DeleteAllOnSubmit(bbs.게시글분류);
                db.게시판.DeleteOnSubmit(bbs);
                db.SubmitChanges();

                return Json(new AjaxSuccessModel(Url.Action("Main", "User")));
            }
        }


    }
}
